package com.tyk.bookstore.back.comment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tyk.bookstore.back.comment.model.entity.CommentDo;
import com.tyk.bookstore.back.comment.model.entity.ReplyDo;
import com.tyk.bookstore.back.comment.model.query.ReplyAddQuery;
import com.tyk.bookstore.back.comment.model.query.ReplyPageQuery;
import com.tyk.bookstore.back.comment.model.result.CommentResultEnum;
import com.tyk.bookstore.back.comment.model.result.ReplyResultEnum;
import com.tyk.bookstore.back.comment.model.vo.ReplyPageVo;
import com.tyk.bookstore.back.comment.repository.CommentRepository;
import com.tyk.bookstore.back.comment.repository.ReplyRepository;
import com.tyk.bookstore.back.comment.service.ReplyService;
import com.tyk.bookstore.back.comment.util.MapStructComment;
import com.tyk.bookstore.back.common.core.model.type.BinVo;
import com.tyk.bookstore.back.common.core.util.AssertUtil;
import com.tyk.bookstore.back.common.core.util.IdGenerator;
import com.tyk.bookstore.back.member.service.MemberApiService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author tyk
 * @description 回复管理
 */
@Service
@RequiredArgsConstructor
public class ReplyServiceImp implements ReplyService {
    private final CommentRepository commentRepository;
    private final ReplyRepository replyRepository;
    private final MemberApiService memberApiService;
    private final TransactionTemplate transactionTemplate;
    private final MapStructComment mapStructComment;


    @Override
    public void reply(ReplyAddQuery query) {

        // 评论存在
        AssertUtil.isTrue(
                commentRepository.exist1Eq(
                        CommentDo::getId, query.getCommentId(), CommentDo.class
                ),
                CommentResultEnum.COMMENT_NOT_FOUND
        );

        // 父级存在
        if (query.getParentId() != 0) {
            AssertUtil.isTrue(
                    replyRepository.exist1Eq(
                            ReplyDo::getId, query.getParentId(), ReplyDo.class
                    ),
                    ReplyResultEnum.REPLY_NOT_FOUND
            );
        }

        // 添加
        transactionTemplate.executeWithoutResult(status -> {
            ReplyDo rd = mapStructComment.toReplyDo(query);
            rd.setId(IdGenerator.id());
            rd.setMemberId(0L);
            replyRepository.insert(rd);
        });

    }

    @Override
    public void deleteById(Long id) {
        AssertUtil.isTrue(
                replyRepository.exist1Eq(
                        ReplyDo::getId, id, ReplyDo.class
                ),
                ReplyResultEnum.REPLY_NOT_FOUND
        );
        replyRepository.deleteById(id);
    }

    @Override
    public ReplyPageVo getPage(ReplyPageQuery query) {

        // 查询
        Page<ReplyDo> page = new Page<>(query.getPage(), query.getLimit());
        LambdaQueryWrapper<ReplyDo> wrapper = Wrappers.lambdaQuery(ReplyDo.class);
        wrapper.eq(ReplyDo::getCommentId, query.getCommentId());
        replyRepository.selectPage(page, wrapper);
        ReplyPageVo rpv = mapStructComment.toReplyPageVo(page);

        // 会员名称
        Map<Long, String> mnMap = memberApiService.getMemberNameListByIdSet(
                rpv.getRecords().stream().map(ReplyPageVo.Reply::getMemberId).collect(Collectors.toSet())
        ).stream().collect(Collectors.toMap(BinVo::getId, BinVo::getValue));

        // 补充
        rpv.getRecords().forEach(it -> it.setMemberName(mnMap.get(it.getMemberId())));

        //返回
        return rpv;
    }
}
